﻿using System;
using Alserve.Ffl;
using Alserve.Ffl.Data;

public partial class ffl_admin_updateplayerscores : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Security.IsAdmin(Page))
            {
                Response.Redirect("~//ffl//anonymous//default.aspx");
            }
            else
            {
                UpdatePlayerScores(DateTime.Today);
                UpdateUserPoints();
            }
        }
    }

    private void UpdatePlayerScores(DateTime dateMonth)
    {
        DateTime startDate = new DateTime(dateMonth.Year,dateMonth.Month,1);
        DateTime endDate = startDate.AddMonths(1).AddMilliseconds(-1);
        DateTime lastMonthStart = startDate.AddMonths(-1);
        DateTime lastMonthEnd = lastMonthStart.AddMonths(1).AddMilliseconds(-1);

        Player thisPlayer = new Player();
        thisPlayer.LoadAll();
        do
        {
            _PlayerScoreView thisPsv = new _PlayerScoreView();
            thisPsv.Where.PlayerId.Value = thisPlayer.Id;

            if (thisPsv.Query.Load())
            {
                Int32 started = 0;
                Int32 playedAsSub = 0;
                Int32 goals = 0;
                Int32 bonusGoals = 0;
                Int32 cleanSheets = 0;
                Int32 concededOne = 0;
                Int32 teamScoredThreePlus = 0;
                Int32 points = 0;
                Int32 pointsThisMonth = 0;
                Int32 pointsLastMonth = 0;

                do
                {

                    points += thisPsv.CalculatedScore;

                    if (thisPsv.MatchDate >= startDate && thisPsv.MatchDate <= endDate)
                        pointsThisMonth += thisPsv.CalculatedScore;

                    if (thisPsv.MatchDate >= lastMonthStart && thisPsv.MatchDate <= lastMonthEnd)
                        pointsLastMonth += thisPsv.CalculatedScore;

                    switch (thisPsv.ScoreType)
                    {
                        case 1:
                            started++;
                            break;
                        case 2:
                            playedAsSub++;
                            break;
                        case 3:
                            cleanSheets++;
                            break;
                        case 4:
                            concededOne++;
                            break;
                        case 5:
                            goals += thisPsv.Amount;
                            bonusGoals += thisPsv.Bonus;
                            break;
                        case 6:
                            teamScoredThreePlus++;
                            break;
                    }

                } while (thisPsv.MoveNext());

                TotalPlayerScores thisTps = new TotalPlayerScores();
                thisTps.Where.PlayerId.Value = thisPlayer.Id;
                if (!thisTps.Query.Load())
                {
                    thisTps.AddNew();
                    thisTps.PlayerId = thisPlayer.Id;
                }
                
                
                thisTps.TotalPoints = points;
                thisTps.ThisMonthPoints = pointsThisMonth;
                thisTps.LastMonthPoints = pointsLastMonth;
                thisTps.Started = started;
                thisTps.PlayedAsSub = playedAsSub;
                thisTps.Goals = goals;
                thisTps.BonusGoals = bonusGoals;
                thisTps.CleanSheets = cleanSheets;
                thisTps.ConcededOne = concededOne;
                thisTps.TeamScoredThreePlus = teamScoredThreePlus;
                thisTps.Save();
            }

        }
        while (thisPlayer.MoveNext());
    }

    private void UpdateUserPoints()
    {
        //Get all paid users
        Alserve.Ffl.Data.User thisUser = new User();
        thisUser.Where.Paid.Value = true;

        if (thisUser.Query.Load())
        {
            while (!thisUser.EOF)
            {
                Int32 totalPoints = 0;
                Int32 pointsThisMonth = 0;
                Int32 pointsLastMonth = 0;
                Int32 positionCount = 0;
                Int32 lastPosition = 0;

                _UserPlayerScores thisUps = new _UserPlayerScores();
                thisUps.Where.UserId.Value = thisUser.Id;

                if (thisUps.Query.Load())
                {
                    while (!thisUps.EOF)
                    {
                        if (thisUps.PositionId != lastPosition)
                            positionCount = 0;

                        lastPosition = thisUps.PositionId;
                        positionCount++;

                        if (thisUps.s_ThisMonthPoints != "")
                            pointsThisMonth += thisUps.ThisMonthPoints;

                        if (thisUps.s_LastMonthPoints != "")
                            pointsLastMonth += thisUps.LastMonthPoints;

                        switch (thisUps.PositionId)
                        {
                            case 1:
                                if (positionCount <= 1 && thisUps.s_TotalPoints != "")
                                    totalPoints += thisUps.TotalPoints;
                                break;

                            case 2:
                                if (positionCount <= 4 && thisUps.s_TotalPoints != "")
                                    totalPoints += thisUps.TotalPoints;
                                break;

                            case 3:
                                if (positionCount <= 3 && thisUps.s_TotalPoints != "")
                                    totalPoints += thisUps.TotalPoints;
                                break;

                            case 4:
                                if (positionCount <= 3 && thisUps.s_TotalPoints != "")
                                    totalPoints += thisUps.TotalPoints;
                                break;
                        }

                        thisUps.MoveNext();
                    }
                    
                }

                //Get manager of the month points
                ManagerOfMonth thisMom = new ManagerOfMonth();
                thisMom.Where.UserId.Value = thisUser.Id;
                if (thisMom.Query.Load())
                {
                    while (!thisMom.EOF)
                    {
                        totalPoints += thisMom.Points;
                        thisMom.MoveNext();
                    }
                }


                UserPoints thisUserPoints = new UserPoints();
                thisUserPoints.Where.UserId.Value = thisUser.Id;
                
                if (!thisUserPoints.Query.Load())
                {
                    thisUserPoints.AddNew();
                }

                thisUserPoints.UserId = thisUser.Id;
                thisUserPoints.TotalPoints = totalPoints;
                thisUserPoints.ThisMonthPoints = pointsThisMonth;
                thisUserPoints.LastMonthPoints = pointsLastMonth;
                thisUserPoints.Save();

                thisUser.MoveNext();
            }
            
        }
    }


}
